<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <title>File Server Test</title>

    <script>
        var pageUrl = location.href;
        var fileserverUrl = pageUrl.substring(0, pageUrl.indexOf("/test.html"))

        function upload(fileObj) {
            console.log("上传的文件：");
            console.log(fileObj);
            var url = fileserverUrl + "/upload";
            // FormData 对象
            var form = new FormData();
            form.append("expire", "false");
            form.append("size", fileObj.size);
            form.append("file", fileObj);
            // XMLHttpRequest 对象
            var xhr = new XMLHttpRequest();
            xhr.open("post", url, true);
            var now = null;
            xhr.onload = function () {
                console.log("耗时：" + (new Date().getTime() - now));
                console.log("上传完成");
            };
            xhr.upload.addEventListener("progress", function (event) {
                console.log(event);
            }, false);
            now = new Date().getTime();
            xhr.send(form);
        }

        function uploadFile() {
            var files = document.getElementById("fileUploader").files;
            if (files.length !== 1) {
                alert("一次性只能发送一个文件")
                return;
            }
            upload(files[0]);
        }

        function downloadFile() {
            location.href = fileserverUrl + "/file/" + document.getElementById("downloadUuid").value;
        }
        function deleteFile() {
            var url = fileserverUrl + "/file/" + document.getElementById("deleteUuid").value;
            // XMLHttpRequest 对象
            var xhr = new XMLHttpRequest();
            xhr.open("delete", url, true);
            xhr.onload = function () {
                console.log("删除完成");
            };
            xhr.send(null);
        }
    </script>
</head>

<body>
<input type="file" id="fileUploader" single/>
<input type="button" value="上传" onclick="uploadFile()"/>
<br/>
<br/>
<input type="text" id="downloadUuid"/>
<input type="button" value="下载" onclick="downloadFile()"/>
<br/>
<br/>
<input type="text" id="deleteUuid"/>
<input type="button" value="删除" onclick="deleteFile()"/>
</body>

</html>
